<?php
namespace app\model;


class Trade {
    
	static function getById($id) {
	    if (empty($id)) {
			return false;
		}
		$sql = "SELECT * FROM " . tablename(t_trade) . " where id = :id";
		$row = db_find($sql, array(":id"=>$id));
		return $row;
	}

    static function getByCommId($commid) {
        if (empty($commid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_trade) . " where ordertype = 'reward' and detailid = :detailid and paystate = 1";
        $row = db_find($sql, array(":detailid"=>$commid));
        return $row;
    }

	static function deleteById($id){
        if (empty($id)) {
            return false;
        }
        db_delete ( t_trade, array ('id' => $id) );
        return true;
    }


	static function getListByCondition($condition,$pageNumber,$pageSize){
        $sql = "SELECT master.* FROM " . tablename ( t_trade ) . " as master
						WHERE   {$condition} ORDER BY master.id desc
									LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;
        $list = db_select ( $sql );
        return $list;

    }

    static function getIncomeByThemeId($themeid){
        $income = db_fetchcolumn ( 'SELECT sum(realfee) FROM ' . tablename ( t_trade ) . "
				WHERE themeid = :themeid and paystate = 1 and ordertype = 'theme' and orderfee > 0 ", array(":themeid"=>$themeid) );
        return floatval($income);
    }

    static function getOutcomeByThemeId($themeid){
        $income = db_fetchcolumn ( 'SELECT sum(realfee) FROM ' . tablename ( t_trade ) . "
				WHERE themeid = :themeid and paystate = 1 and ordertype = 'reward' and orderfee < 0 ", array(":themeid"=>$themeid) );
        return floatval($income);
    }


    static function getNumByCondition($condition){
        return db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_trade ) . " as master
					WHERE  {$condition} ORDER BY master.id" );

    }


    static function getCommByOpenId($id,$openid) {
        if (empty($id)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_trade) . " 
                        where  openid = :openid and paystate = 1 and ordertype = 'comm' order by id desc";
        $row = db_select($sql, array(":openid"=>$openid));
        return $row;
    }


    static function getCommNumByOpenid($openid,$uniacid){
        $params = array();
        $params[':uniacid'] = $uniacid;
        $params[':openid'] = $openid;
        $point = db_fetchcolumn("SELECT count(*) FROM ".tablename(t_trade)."
			WHERE uniacid= :uniacid and openid = :openid and paystate = 1 and ordertype ='comm'",$params);
        return intval($point);
    }

    static function getOrderSumByOpenid($openid,$uniacid){
        $params = array();
        $params[':uniacid'] = $uniacid;
        $params[':openid'] = $openid;
        $point = db_fetchcolumn("SELECT count(*) FROM ".tablename(t_trade)."
			WHERE uniacid= :uniacid and openid = :openid and status >= 0",$params);
        return intval($point);
    }

    static function getByPaystateOrderId($id) {
        if (empty($id)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_trade) . " 
                        where orderid = :id and paystate = 1 and ordertype = 'repair' order by id desc";
        $row = db_find($sql, array(":id"=>$id));
        return $row;
    }
    static function getByOrderIdAndOpenid($themeid,$orderid,$openid) {
        if (empty($openid) || empty($orderid) || empty($themeid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_trade) . " 
                        where themeid = :themeid and masterid = :masterid and openid = :openid 
                                and orderfee > 0 and ordertype = 'theme' order by id desc limit 1";
        $row = db_find($sql, array(":themeid"=>$themeid,":masterid"=>$orderid,":openid"=>$openid));
        return $row;
    }
    static function getByOrderId($id,$openid) {
        if (empty($id)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_trade) . " 
                        where orderid = :id and openid = :openid and paystate = 1 and ordertype = 'order' order by id desc";
        $row = db_find($sql, array(":id"=>$id,":openid"=>$openid));
        return $row;
    }
    static function getByMixId($openid,$orderid,$uniacid){
        if (empty($openid) || empty($orderid) || empty($uniacid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_trade) . " 
                        where openid = :openid and orderid =:orderid and uniacid =:uniacid order by id desc";
        $row = db_find($sql, array(":openid"=>$openid,":orderid"=>$orderid,":uniacid"=>$uniacid));
        return $row;

    }
}
?>